package com.atlige.gzmall.cart.controller;

import com.atlige.common.constant.AuthServerConstant;
import com.atlige.gzmall.cart.interceptor.CartInterceptor;
import com.atlige.gzmall.cart.service.CartService;
import com.atlige.gzmall.cart.vo.Cart;
import com.atlige.gzmall.cart.vo.CartItem;
import com.atlige.gzmall.cart.vo.UserInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author: Jeremy lI
 * @description:
 * @date: created in 2021/6/5 23:32
 */
@Controller
public class CartController {
    @Autowired
    CartService cartService;

    /**
     * 获取当前用户选中的购物项
     * @return
     */
    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public List<CartItem> getCurrentUserCartItems(){
        return cartService.getUserCartItems();
    }
    /**
     * 浏览器有一个cookie：user-key:用来标识用户身份，一个月后过期，如果用户第一次使用jd的购物车功能，
     * 都会给一个临时用户身份，浏览器以后保存，每次访问都会带上这个cookie
     * 登录：session有
     * 没登陆：按照cookie里面的user-key来做
     * 第一次：创建临时用户
     * @param model
     * @return
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        // 快速获取用户信息
//        UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
        Cart cart = cartService.getCart();
        model.addAttribute("cart", cart);
        return "cartList";
    }

    /**
     * 添加商品到购物车
     * ra.addAttribute: 将数据放在url后面
     * ra.addFlashAttribute：将数据放在session中，可以从页面取出，但只能取一次
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes ra) throws ExecutionException, InterruptedException {
        CartItem cartItem = cartService.addToCart(skuId, num);
        ra.addAttribute("skuId", skuId);
        return "redirect:http://cart.gzmall.com/addToCartSuccess.html";
    }

    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId, Model model){
        // 重定向到成功页面，再次查询购物车数据即可
        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("cartItem", cartItem);
        return "success";
    }

    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("checked") Integer checked){
        cartService.checkItem(skuId, checked);
        return "redirect:http://cart.gzmall.com/cart.html";
    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num){
        cartService.changeItemCount(skuId, num);
        return "redirect:http://cart.gzmall.com/cart.html";
    }

    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gzmall.com/cart.html";
    }

}
